Skip to content

Conversation

robertky
Copy link
Member

Closes #15

Summary

  • Sends a persistent browser-local UUID as X-User-Id with GraphQL requests and validates it in the server context.
  • Creates the simulated user server-side when they save their first review.
  • Adds upsertReview and deleteReview, enforcing one review per user and album.
  • Adds an accessible review form on the detail page: labelled 1–5 star radio group, optional comment, inline role="alert" errors, update and delete.
  • Refetches album details after a review change so the current user review and review count stay current.

Verification

  • pnpm format:check
  • pnpm lint
  • client and server typecheck
  • 57 client tests and 33 server tests
  • pnpm -r build

Stacked PR

This PR targets #57 (feat/11-album-details) because it adds interactive reviews to the detail page delivered there. Review and merge #57 first; then this PR can be retargeted to main.

@robertky robertky requested a review from a team as a code owner October 22, 2026 09:55
Copy link
Member

@evenkkl evenkkl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review

Three things to fix before merging:

1. The app will crash on our server
crypto.randomUUID() only works on HTTPS or localhost. Our VM runs on plain HTTP (http://it2810-19.idi.ntnu.no), so getUserId() throws and the GraphQL requests fail.
Fix: If crypto.randomUUID isn't available, make the UUID another way, for example with crypto.getRandomValues.

export function getUserId(): string {
const existing = window.localStorage.getItem(USER_ID_STORAGE_KEY);
if (isUuid(existing)) return existing.toLowerCase();
const userId = crypto.randomUUID();
window.localStorage.setItem(USER_ID_STORAGE_KEY, userId);
return userId;
}

2. Album links don't work on the server
The app lives under /project2/, but the links go to /album/1 and /. They leave the app, and the /^\/album\/(\d+)$/ check never matches /project2/album/1.
Fix: Start links with import.meta.env.BASE_URL, and remove that prefix from the path before running the regex.

export default function App() {
const detailMatch = /^\/album\/(\d+)$/.exec(window.location.pathname);
const detailId = detailMatch?.[1];

<h3 id={`album-${album.id}`}>
<a href={`/album/${album.id}`}>{album.title}</a>
</h3>

3. The page flashes "Loading album…" after every save or delete
refetch() sets loading back to true, so the whole page is replaced by the loading text and focus is lost. That's bad for accessibility.
Fix: Change if (loading) to if (loading && !data).

export default function AlbumDetailPage({ id }: { id: string }) {
const { data, loading, error, refetch } = useQuery(ALBUM_DETAIL, {
variables: { id, reviewFirst: REVIEW_PAGE_SIZE, reviewAfter: null },
});
if (loading) {
return (
<main id="main" className="album-detail" tabIndex={-1} aria-busy="true">
<p role="status">Loading album…</p>
</main>
);
}

Small extra: when a review is edited, updatedAt isn't updated. Add updatedAt: new Date() to the set in onConflictDoUpdate.

})
.onConflictDoUpdate({
target: [review.userId, review.albumId],
set: { rating: input.rating, comment: input.comment.trim() },
})
.returning();

Copy link
Member

@erikhfj erikhfj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Reviewed as the delta against feat/11-album-details, since this is stacked on #57.

The identity contract is the part I checked hardest, and it holds up. X-User-Id is attached with setContext around the HttpLink, so it goes on every request and not only the mutations. parseUserId runs per request in the Yoga context and nothing is hoisted to module scope, so one browser's identity cannot leak into another request. myReviewByAlbum degrades to all-null when there is no user, which is the anonymous behaviour we agreed on.

removeReview scopes the delete with and(eq(review.id, id), eq(review.userId, ownerId)), so a user cannot delete someone else's review. onConflictDoUpdate targeting [review.userId, review.albumId] enforces one review per user per album without a round trip. ensureUser runs after findAlbum, so a bad album id does not leave an orphan user behind. Good.

The star input is a real radio group: fieldset, legend, shared name, and each input wrapped in its own label. That satisfies the radio-group requirement in #17 as written.

Findings

1. updatedAt in the upsert is dead code (low)

.set({ rating: ..., comment: ..., updatedAt: new Date() })

review_set_updated_at is a BEFORE UPDATE trigger doing NEW.updated_at := now(), so it overwrites whatever is set here. Harmless, but it reads as if the resolver owns updated_at when the trigger does. Same class of confusion as average_rating, which is why I am calling it out: #12 is adding a trigger-owned average_rating, and the resolver should not write that either.

Worth deleting the line and leaving a comment that the trigger owns it.

2. Delete is one click and irreversible (medium-low)

remove() fires immediately with no confirmation. A review with a written comment is gone on a stray click. A confirm step, or an undo window, would be kinder.

3. catch {} throws away the server's message (medium-low)

Both handlers swallow the error and show a generic string. The server has specific, actionable messages - A valid X-User-Id header is required for this action, Rating must be an integer between 1 and 5. The user currently gets "Your review could not be saved" for all of them. Surfacing error.message when it is a GraphQLError would be cheap.

4. Comment length is unbounded (medium-low)

Neither the textarea nor saveReview limits the comment. This is a public write path, so a very large payload is a free way to bloat the table and the detail page. A maxLength on the input plus a server-side check would close it.

5. Two plain Error throws (low)

if (!saved) throw new Error('Could not save review.') and the equivalent in ensureUser. Everything else in the module uses GraphQLError, and a plain error surfaces as an internal server error rather than a clean client-facing one.

6. localStorage access is unguarded (low)

getUserId() calls window.localStorage.getItem and setItem directly, and it sits on the request path for every call. In a restricted browsing context that can throw, which would fail every request rather than one. A try/catch falling back to an in-memory id would make it degrade instead.

On merging

Nothing here blocks the merge in my view. 1, 3 and 4 are small and worth folding in while the file is fresh. 2 and 6 are judgement calls. 5 is consistency.

Copy link
Member

@rachelks rachelks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the previous feedback! 😊 The functionality looks good overall.

I found two remaining issues that I think should be fixed before merging, and I've left inline comments on both.

One small UI observation from testing locally: the album detail page and review form look a bit different from our UI sketch, particularly the layout, rating inputs and "Back to discovery" link. Some styling adjustments might be worth considering, either in this PR or as part of the final UI polish. I'll leave that up to you, as it's not a blocker for this PR!

@robertky robertky force-pushed the feat/15-reviews branch 3 times, most recently from a7d1ce3 to eef4e8b Compare October 26, 2026 00:39
rachelks
rachelks previously approved these changes Oct 26, 2026
Copy link
Member

@rachelks rachelks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now! Thanks for addressing the feedback 😊

@robertky robertky changed the base branch from feat/11-album-details to main October 26, 2026 09:08
@robertky robertky dismissed rachelks’s stale review October 26, 2026 09:08

The base branch was changed.

Copy link
Member

@rachelks rachelks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good after updating the base branch. Ready to merge! 😊

@rachelks rachelks merged commit 64be6ef into main Oct 26, 2026
2 checks passed
@rachelks rachelks deleted the feat/15-reviews branch October 29, 2026 15:37
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reviews: upsert and delete
4 participants